package ModelLayer;

import java.util.HashMap;
import java.util.ArrayList;
/**
 * LoanCon functions as a container for Loan objects. It's set up as a singleton, to avoid the creation of several DVDCon objects.
 *
 * @author Anders A.P, Anna K., Danny M.P., Jakob K., Mie P.  
 * @version 1.0
 */
public class LoanCon
{
    private HashMap<Integer, Loan> loanCon;
    private static LoanCon instance;

    /**
     * Constructs one, and is limited to one construction of class LoanCon, taking no parameters but initializing loanCon collection.
     */
    private LoanCon()
    {
        loanCon = new HashMap<Integer, Loan>(); 
    }
    
    /**
     * creates a singleton instance which through its constructor creates the singleton collection.
     *
     * @return  instance    Returns the singleton instance to the caller.
     */
    public static LoanCon getInstance()
    {        
        if(instance == null) {          
            instance = new LoanCon();
        }        
        return instance;
    }
    
    /**
     * Adds a Loan object to the singleton collection. Uses Loan ID and Loan object as key and value respectively.
     *
     * @param   loan    An instance of class Loan.
     */
    public void addLoan(Loan loan)
    {        
        loanCon.put(loan.getID(), loan);
        
    }
    
    /**
     * Finds a Loan object in the singleton collection by ID key.
     *
     * @param   id          The mapping key, used for finding a Loan object value mapped to this key specifically.
     * @return  Loan        Returns a DVD object mapped to the key value supplied as a parameter.
     * @throw   Exception   Throws an exception caught by the UI if no Loan object is mapped to the key.
     */
    public Loan getLoan(int id)
    {
        if(loanCon.get(id) != null) {
            return loanCon.get(id);
        }
        else {
            throw new IllegalArgumentException("Loan not found");
        }        
    }

    /**
     * Returns the HashMap collection to the caller.
     *
     * @return  HashMap Returns the singleton HashMap collection to the caller.
     */    
    public HashMap<Integer, Loan> getMap()
    {        
        return loanCon;        
    }
    
    /**
     * Returns a list of all Loan objects in the singleton collection.
     *
     * @return  ArrayList   Returns a list view of all Loan objects in the HashMap.
     */
    public ArrayList<Loan> listAll()
    {
        ArrayList<Loan> loans = new ArrayList<Loan>();
        for(Loan l : loanCon.values()) {            
            loans.add(l);
        }
        return loans;        
    }

    /**
     * Finds all instances of Loan in the singleton collection by looking at the values in the HashMap.
     *
     * @param   name        Parameter for equating Loan name with supplied name.
     * @return  ArrayList   Returns a list view of all Loan objects matching the given name.
     * @throw   Exception   If no Loan contains the name supplied.
     */    
    public ArrayList<Loan> getLoanName(String name)
    {
        ArrayList<Loan> loans = new ArrayList<Loan>();
        for(Loan l : loanCon.values()) {
            if(l.getPerson().getName().equals(name)){
                loans.add(l);
            }
        }
        if(loans != null) {
            return loans;
        }
        else {
            throw new IllegalArgumentException("Loans not found.");
        }
    }       
}
